using System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint;

namespace PublishingImageWebPart
{
    public class AssetUrlSelectorEditorPart : EditorPart
    {
        private Microsoft.SharePoint.Publishing.WebControls.AssetUrlSelector selector = null;

        #region AssetUrlProperty
        private string _AssetUrlProperty = null;

        public string AssetUrlProperty
        {
            get { return _AssetUrlProperty; }
            set { _AssetUrlProperty = value; }
        }
        #endregion

        #region UseImageAssetPicker
        private bool _UseImageAssetPicker = false;

        public bool UseImageAssetPicker
        {
            get { return _UseImageAssetPicker; }
            set { _UseImageAssetPicker = value; }
        }
        #endregion

        #region AssetPickerButtonVisible
        private bool _AssetPickerButtonVisible = false;

        public bool AssetPickerButtonVisible
        {
            get { return _AssetPickerButtonVisible; }
            set { _AssetPickerButtonVisible = value; }
        }
        #endregion

        #region PickerButtonText
        private string _PickerButtonText = null;

        public string PickerButtonText
        {
            get { return _PickerButtonText; }
            set { _PickerButtonText = value; }
        }
        #endregion

        #region LabelText
        private string _LabelText = null;

        public string LabelText
        {
            get { return _LabelText; }
            set { _LabelText = value; }
        }
        #endregion

        #region ToolTipText
        private string _ToolTipText = null;

        public string ToolTipText
        {
            get { return _ToolTipText; }
            set { _ToolTipText = value; }
        }
        #endregion

        protected string AssetUrl
        {
            get
            {
                System.Reflection.PropertyInfo propertyinfo = WebPartToEdit.GetType().GetProperty(AssetUrlProperty);
                if (propertyinfo == null) throw new ArgumentOutOfRangeException("AssetUrlProperty");

                object value = propertyinfo.GetValue(WebPartToEdit, null);
                return value != null ? value.ToString() : null;
            }
            set
            {
                System.Reflection.PropertyInfo propertyinfo = WebPartToEdit.GetType().GetProperty(AssetUrlProperty);
                if (propertyinfo == null) throw new ArgumentOutOfRangeException("AssetUrlProperty");

                propertyinfo.SetValue(WebPartToEdit, value, null);
            }
        }

        /// <summary>
        /// Creates the control cell.
        /// </summary>
        /// <param name="control">The control.</param>
        /// <param name="labelText">The label text.</param>
        /// <param name="toolTipText">The tool tip text.</param>
        /// <returns>Formatted TableCell</returns>
        private static TableCell CreateControlCell(Control control, string labelText, string toolTipText)
        {
            TableCell cell = new TableCell();

            Panel panelHeader = new Panel();
            cell.Controls.Add(panelHeader);
            panelHeader.CssClass = "UserSectionHead";

            Label label = new Label();
            panelHeader.Controls.Add(label);
            label.Text = labelText;
            label.AssociatedControlID = control.ID;
            if (!string.IsNullOrEmpty(toolTipText))
            {
                label.ToolTip = toolTipText;
            }

            Panel panelBody = new Panel();
            cell.Controls.Add(panelBody);
            panelBody.CssClass = "UserSectionBody";

            Panel panelGroup = new Panel();
            panelBody.Controls.Add(panelGroup);
            panelGroup.CssClass = "UserControlGroup";
            panelGroup.Controls.Add(control);

            Panel panelLine = new Panel();
            cell.Controls.Add(panelLine);
            panelLine.CssClass = "UserDottedLine";

            return cell;
        }

        /// <summary>
        /// Creates the list names section.
        /// </summary>
        /// <returns>TableRow object</returns>
        private TableRow CreateListSection()
        {
            TableRow row = new TableRow();

            selector = new Microsoft.SharePoint.Publishing.WebControls.AssetUrlSelector();
            selector.AssetUrlChanged += new EventHandler(selector_AssetUrlChanged);

            selector.UseImageAssetPicker = UseImageAssetPicker;
            selector.AssetPickerButtonVisible = AssetPickerButtonVisible;
            selector.PickerButtonText = PickerButtonText;

            TableCell cell = CreateControlCell(selector, LabelText, ToolTipText);
            row.Cells.Add(cell);
            return row;
        }

        protected override void CreateChildControls()
        {
            Table table = new Table();
            Controls.Add(table);
            table.Width = Unit.Percentage(100);
            table.CellPadding = 0;
            table.CellSpacing = 0;
            table.BorderWidth = 0;

            TableRow row1 = CreateListSection();
            table.Rows.Add(row1);
        }

        void selector_AssetUrlChanged(object sender, EventArgs e)
        {
            if (selector != null)
            {
                AssetUrl = selector.AssetUrl;
            }
        }

        public override void SyncChanges()
        {
            EnsureChildControls();
            if (selector != null)
            {
                selector.AssetUrl = null;

                if (!string.IsNullOrEmpty(AssetUrl))
                {
                    selector.AssetUrl = AssetUrl;
                }
            }
        }


        public override bool ApplyChanges()
        {
            if (selector != null)
            {
                AssetUrl = selector.AssetUrl;
            }
            else return false;
            return true;
        }

        public AssetUrlSelectorEditorPart(string AssetUrlProperty, string title)
        {
            this.AssetUrlProperty = AssetUrlProperty;
            this.Title = title;
        }

    }
}
